###############################################################################
# SuperLinks                                                                  #
# Written by Gregory Nolle (greg@nolle.co.uk)                                 #
# Copyright 2000-2003 by PlasmaPulse Solutions (http://www.plasmapulse.com)   #
###############################################################################
# Libraries/HTML/Form.pm -> Form html library                                 #
###############################################################################
# DON'T EDIT BELOW UNLESS YOU KNOW WHAT YOU'RE DOING!                         #
###############################################################################
package HTML::Form;

BEGIN { require "System.pm.pl"; import System qw($SYSTEM); }

use strict;

sub new {
  my $class = shift;
  my $self  = { };
  $SL::CGI->autoEscape(0);
  return bless($self, $class);
}

sub DESTROY { }

################################################################################
# Form subroutine
sub Form {
  my $self = shift;
  my %in = (
    "name"      => "form",
    "method"    => "POST",
    "action"    => $SYSTEM->{'SCRIPT_URL'},
    "body"      => "",
    "multipart" => 0,
    "hiddens"   => [], # hidden: { name => "", value => "" }
    "extra"     => {},
    @_
  );

  my $Return = $SL::CGI->start_form(
    -method   => $in{'method'},
    -action   => $in{'action'},
    -enctype  => ($in{'multipart'} ? "multipart/form-data" : ""),
    name      => $in{'name'},
    %{ $in{'extra'} }
  );

  foreach my $hidden (@{ $in{'hiddens'} }) {
    $Return .= $SL::CGI->hidden(-name => $hidden->{'name'}, -default => $hidden->{'value'}, -override => 1);
  }
  
  $Return .= $in{'body'};
  $Return .= $SL::CGI->endform();
  
  return $Return;
}

################################################################################
# TextBox subroutine
sub TextBox {
  my $self = shift;
  my %in = (
    "name"      => "",
    "value"     => "",
    "class"     => "form-textbox",
    "style"     => "",
    "extra"     => {},
    "password"  => 0,
    "empty"     => 0,
    @_
  );

  my %Extra       = %{ $in{'extra'} };
  $Extra{'class'} = $in{'class'};
  $Extra{'style'} = $in{'style'};
  
  if ($in{'password'}) {
    return $SL::CGI->password_field(-name => $in{'name'}, -default => $in{'value'}, -override => $in{'empty'}, %Extra);
  } else {
    return $SL::CGI->textfield(-name => $in{'name'}, -default => $in{'value'}, -override => $in{'empty'}, %Extra);
  }
}

################################################################################
# FileBox subroutine
sub FileBox {
  my $self = shift;
  my %in = (
    "name"      => "",
    "value"     => "",
    "class"     => "form-textbox",
    "style"     => "",
    "extra"     => {},
    "password"  => 0,
    "empty"     => 0,
    @_
  );

  my %Extra       = %{ $in{'extra'} };
  $Extra{'class'} = $in{'class'};
  $Extra{'style'} = $in{'style'};

  return $SL::CGI->filefield(-name => $in{'name'}, -default => $in{'value'}, -override => $in{'empty'}, %Extra);
}

################################################################################
# TextArea subroutine
sub TextArea {
  my $self = shift;
  my %in = (
    "name"      => "",
    "value"     => "",
    "class"     => "form-textarea",
    "style"     => "",
    "extra"     => {},
    "rows"      => 1,
    "empty"     => 0,
    @_
  );

  my %Extra       = %{ $in{'extra'} };
  $Extra{'class'} = $in{'class'};
  $Extra{'style'} = $in{'style'};
  
  return $SL::CGI->textarea(-name => $in{'name'}, -default => $in{'value'}, -override => $in{'empty'}, -rows => $in{'rows'}, %Extra);
}

################################################################################
# Radio subroutine
sub Radio {
  my $self = shift;
  my %in = (
    "name"      => "",
    "value"     => "",
    "items"     => [], # item: { value => "", label => "", extra => "" }
    "join"      => "",
    "empty"     => 0,
    @_
  );

  my @Items;
  foreach my $item (@{ $in{'items'} }) {
    $item->{'extra'} ||= {};
    
    my $return  = qq~<font class="form-label">~;
       $return .= $SL::CGI->radio_group(-name => $in{'name'}, -value => $item->{'value'}, -default => $in{'value'}, -override => $in{'empty'}, -nolabels => 1, %{ $item->{'extra'} });
       $return .= qq~ $item->{'label'}~ if ($item->{'label'});
       $return .= qq~</font>~;
    push(@Items, $return);
  }

  return join($in{'join'}, @Items);
}

################################################################################
# CheckBox subroutine
sub CheckBox {
  my $self = shift;
  my %in = (
    "name"      => "",
    "value"     => "",
    "items"     => [], # item: { value => "", label => "", extra => "" }
    "join"      => "",
    "empty"     => 0,
    @_
  );

  my @Items;
  foreach my $item (@{ $in{'items'} }) {
    $item->{'extra'} ||= {};
    
    my $return  = qq~<font class="form-label">~;
       $return .= $SL::CGI->checkbox_group(-name => $in{'name'}, -value => $item->{'value'}, -default => $in{'value'}, -override => $in{'empty'}, -nolabels => 1, %{ $item->{'extra'} });
       $return .= qq~ $item->{'label'}~ if ($item->{'label'});
       $return .= qq~</font>~;
    push(@Items, $return);
  }

  return join($in{'join'}, @Items);
}

################################################################################
# SelectBox subroutine
sub SelectBox {
  my $self = shift;
  my %in = (
    "name"      => "",
    "value"     => "",
    "class"     => "form-selectbox",
    "style"     => "",
    "extra"     => {},
    "items"     => [], # item: { value => "", label => "" }
    "rows"      => 1,
    "multiple"  => 0,
    "empty"     => 0,
    @_
  );

  my (@Values, %Labels, %Extra);

  foreach my $item (@{ $in{'items'} }) {
    push(@Values, $item->{'value'});
    $Labels{ $item->{'value'} } = $item->{'label'};
  }
  
  my %Extra       = %{ $in{'extra'} };
  $Extra{'class'} = $in{'class'};
  $Extra{'style'} = $in{'style'};

  if ($in{'multiple'} || $in{'rows'} > 1) {
    return $SL::CGI->scrolling_list(-name => $in{'name'}, values => \@Values, -default => $in{'value'}, -override => $in{'empty'}, -size => $in{'rows'}, -multiple => $in{'multiple'}, -labels => \%Labels, %Extra);
  } else {
    return $SL::CGI->popup_menu(-name => $in{'name'}, values => \@Values, -default => $in{'value'}, -override => $in{'empty'}, -labels => \%Labels, %Extra);
  }
}

################################################################################
# Button subroutine
sub Button {
  my $self = shift;
  my %in = (
    "items"     => [], # item: { type => "", name => "", value => "", label => "", src => "", extra => "" }
    "join"      => "",
    @_
  );

  my @Items;
  foreach my $item (@{ $in{'items'} }) {
    my $return  = qq~<font class="form-label"><input type="$item->{'type'}" value="$item->{'value'}"~;
       $return .= qq~ src="$item->{'src'}"~ if ($item->{'src'});
       $return .= qq~ name="$item->{'name'}"~ if ($item->{'name'});
    
    if ($item->{'extra'}) {
      foreach my $key (keys %{ $item->{'extra'} }) {
        if ($key eq "disabled") {
          $return .= qq~ disabled~;
        } else {
          $return .= qq~ $key="$item->{'extra'}->{$key}"~;
        }
      }
    }
    
    $return .= qq~> $item->{'label'}</font>~;
    push(@Items, $return);
  }

  return join($in{'join'}, @Items);
}

1;